//
//  MPLockView.m
//  MPLockView
//
//  Created by 周晓瑞 on 2017/3/23.
//  Copyright © 2017年 colleny. All rights reserved.
//

#import "MPLockView.h"

@interface MPLockView ()
@property(nonatomic,strong)NSMutableArray *selectedBtnArray;
@property(nonatomic,assign)CGPoint currentMovePoint;
@end

@implementation MPLockView

- (NSMutableArray *)selectedBtnArray{
    if(!_selectedBtnArray){
        _selectedBtnArray = [NSMutableArray array];
    }
    return _selectedBtnArray;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self commitGrimView];
}

- (void)commitGrimView{
    
    int coloum = 3;
    int row = 3;
    for (int i = 0; i<coloum*row; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor=[UIColor whiteColor];
        btn.userInteractionEnabled = NO;
        [btn setImage:[UIImage imageNamed:@"diy_button_order"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"head_color"] forState:UIControlStateSelected];
        btn.tag = i;
        [self addSubview:btn];
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    int coloum = 3;
    CGFloat orginX = 0;
    CGFloat orginY = 0;
    CGFloat btnWidth = 60;
    CGFloat margin = (self.bounds.size.width - coloum *btnWidth)/4.0;

    for (int i = 0; i<self.subviews.count;i++) {
        UIButton *btn = self.subviews[i];
        int j = i%3;
        int k = i/3;
        orginX = margin + (margin + btnWidth)*j;
        orginY = margin + (margin + btnWidth)*k;
        btn.frame = CGRectMake(orginX, orginY, btnWidth, btnWidth);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    CGPoint startP = [touch locationInView:self];
    for (UIButton *btn in self.subviews) {
        if(CGRectContainsPoint(btn.frame,startP)&&!btn.selected){
             btn.selected = YES;
            [self.selectedBtnArray addObject:btn];
        }
    }
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentP = [touch locationInView:self];
    self.currentMovePoint = currentP;
    for (UIButton *btn in self.subviews) {
        if(CGRectContainsPoint(btn.frame,currentP)&&!btn.selected){
            btn.selected = YES;
            [self.selectedBtnArray addObject:btn];
        }else{
            
        }
    }
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSMutableString *totalStr = [NSMutableString string];
    for (UIButton *btn  in self.selectedBtnArray) {
        btn.selected = NO;
        [totalStr appendString:[NSString stringWithFormat:@"%ld",(long)btn.tag]];
    }
    [self.selectedBtnArray removeAllObjects];
    [self setNeedsDisplay];
    
    NSLog(@"最终的手势密码为----%@",totalStr);
}

- (void)drawRect:(CGRect)rect {
    
    if(self.selectedBtnArray.count<=0)
        return;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath setLineWidth:10];
    [bezierPath setLineCapStyle:kCGLineCapRound];
    [[UIColor redColor]setStroke];
    [bezierPath setLineJoinStyle:kCGLineJoinRound];
    for (int i=0;i<self.selectedBtnArray.count;i++) {
        UIButton *btn = self.selectedBtnArray[i];
        if(i==0){
            [bezierPath moveToPoint:btn.center];
        }else{
            [bezierPath addLineToPoint:btn.center];
        }
    }
    
    [bezierPath addLineToPoint:self.currentMovePoint];
    [bezierPath stroke];
}

@end
